package hz.mobileproject.pda.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.entity.request.Request_Edu_inter;

/**
 * Created by hasee on 2017/10/17.
 */

public class UploadFailListAdapter extends BaseAdapter {

    private List<Request_Edu_inter> mList;

    private LayoutInflater mInflater;

    private Context mContext;

    public UploadFailListAdapter(Context context, List<Request_Edu_inter> list){
        mContext = context;
        mList = list;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList == null ? null : mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if(view == null){
            vh = new ViewHolder();
            view = mInflater.inflate(R.layout.upload_fail_list_item, null);
            vh.tvPoliceName = (TextView) view.findViewById(R.id.tv_upload_fail_police_name);
            vh.tvPrisonerName = (TextView) view.findViewById(R.id.tv_upload_fail_prisoner_name);
            vh.tvTime = (TextView) view.findViewById(R.id.tv_upload_fail_police_time);
            vh.tvTalkLong = (TextView) view.findViewById(R.id.tv_upload_fail_talk_long);
            vh.tvType = (TextView) view.findViewById(R.id.tv_upload_fail_police_type);
            vh.rlProgress = (RelativeLayout) view.findViewById(R.id.rl_upload_fail_progress);
            view.setTag(vh);
        }else{
            vh = (ViewHolder) view.getTag();
        }

        vh.tvPoliceName.setText(mList.get(i).getPoliceName());
        vh.tvPrisonerName.setText(mList.get(i).getPrisonerName());
        vh.tvTime.setText(mList.get(i).getTalkTime());
        String timeLong = mList.get(i).getTalkTimeLong();
        vh.tvTalkLong.setText(changeStringTime(timeLong));
        vh.tvType.setText(mList.get(i).getmTalkClassify() + " _ " + mList.get(i).getmTalkType());
        if(mList.get(i).getUp_down().equals("2")){
            vh.rlProgress.setVisibility(View.VISIBLE);
        }else{
            vh.rlProgress.setVisibility(View.GONE);
        }

        return view;
    }

    private String changeStringTime(String time) {
        long longTime = Long.parseLong(time);
        try {
            Date date = new Date(longTime);
            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
            formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
            return formatter.format(date);
        }catch (Exception e){
            e.printStackTrace();
        }

        return "";
    }

    static class ViewHolder{
        TextView tvPoliceName;
        TextView tvPrisonerName;
        TextView tvTime;
        TextView tvTalkLong;
        TextView tvType;
        RelativeLayout rlProgress;
    }

}
